<template>
  <div ref="wrapper">
    <slot></slot>
  </div>
</template>

<script>
  import BScroll from 'better-scroll'

  export default {
    name: 'scroll',
    components: {},
    data() {
      return {}
    },
    props: {
      probeType: {
        type: Number,
        default: 1
      },
      click: {
        type: Boolean,
        default: true
      },
      data: {
        type: Array,
        default: null
      },
      listenScroll: {
        type: Boolean,
        default: false
      },
      pullup: {
        type: Boolean,
        default: false
      },
      beforScroll: {
        type: Boolean,
        default: false
      },
      refreshDelay: {
        type: Number,
        default: 100
      }
    },
    watch: {
      data() {
        setTimeout(() => {
          this.refresh()
        }, this.refreshDelay)
      }
    },
    methods: {
      _initScroll() {
        if (!this.$refs.wrapper) {
          return
        }

        this.scroll = new BScroll(this.$refs.wrapper, {
          probeType: this.probeType,
          click: this.click,
/*
          preventDefault: false,
*/

          probeType: 3
        })
        if (this.listenScroll) {
          this.scroll.on('scroll', (pos) => {
            this.$emit('scroll', pos)
          })
        }
        if (this.pullup) {
          this.scroll.on('scrollEnd', () => {
            if (this.isloading) {
              return
            }
            if (this.scroll.y <= this.scroll.maxScrollY + 50) {
              this.isloading = true
              this.$emit('scrollToEnd')
            }
          })
        }
        if (this.beforScroll) {
          this.scroll.on('beforScrollStart', (pos) => {
            this.$emit('beforScroll', pos)
          })
        }
      },
      enable() {
        this.scroll && this.scroll.enable()
      },
      disable() {
        this.scroll && this.scroll.disable()
      },
      refresh() {
        this.scroll && this.scroll.refresh()
        this.isloading = false
      },
      scrollto() {
        //多个参数传过去而不用管参数的长度
        this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments)
      },
      scrollToElement() {
        //多个参数传过去而不用管参数的长度
        this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments)
      }
    },
    computed: {},
    created() {
    },
    mounted() {
      this.$nextTick(() => {
        this.isloading = false
        this._initScroll()
      })
    },
    destroyed() {
    }
  }
</script>

<style lang="stylus" scoped>
</style>
